package com.ctbri.manage.file.util;

import java.util.List;

public class PageUtils<T> {
	/**
	 * 数据集
	 */
	private final List<T> ITEMS;

	/**
	 * Object数据
	 */
	private final Object OBJECT_ITEM;

	/**
	 * 总条数
	 */
	private final int TOTAL;

	private final int PAGE_INDEX;
	private final int PAGE_SIZE;

	private int status;
	private String message;

	public PageUtils(List<T> ITEMS, int TOTAL, Integer page_index, Integer page_size, Object object_item) {
		this.ITEMS = ITEMS;
		this.TOTAL = TOTAL;
		PAGE_INDEX = (page_index == null || page_index == 0) ? 1 : page_index;
		PAGE_SIZE = (page_size == null || page_size <= 0) ? 10 : page_size;
		OBJECT_ITEM = object_item;
	}

	public PG<T> paging() {
		Page<T> page = new Page<T>();
		page.setDataList(ITEMS);
		page.setDataObject(OBJECT_ITEM);

		page.setPageSize(PAGE_SIZE);
		page.setPageIndex(PAGE_INDEX);
		page.setTotalSize(TOTAL);

		page.setStatus(status);
		page.setMessage(message);

		PG<T> paging = paging(page);

		return paging;
	}

	private PG<T> paging(Page<T> page) {
		List<T> items = page.getDataList();
		PG pg = new PG();
		pg.setData(items);
		pg.setPageIndex(page.getPageIndex());
		pg.setTotalPage(page.getTotalPage());
		pg.setDataObject(page.getDataObject());
		pg.setMessage(page.getMessage());
		pg.setStatus(page.getStatus());
		pg.setTotalCount(TOTAL);

		boolean pagingAble = false;
		if (items != null) {
			if (items.size() > 0)
				pagingAble = true;
		}
		pg.setPagingAble(pagingAble);
		return pg;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public class PG<T> {
		private List<T> data;
		private int pageIndex;
		private int totalPage;
		/**
		 * 总条数
		 */
		private int totalCount;
		private boolean pagingAble;

		private int status;
		private String message;
		

		private String pageShow;

		private Object dataObject;

		public List<T> getData() {
			return data;
		}

		public void setData(List<T> data) {
			this.data = data;
		}

		public int getPageIndex() {
			return pageIndex;
		}

		public void setPageIndex(int pageIndex) {
			this.pageIndex = pageIndex;
		}

		public int getTotalPage() {
			return totalPage;
		}

		public void setTotalPage(int totalPage) {
			this.totalPage = totalPage;
		}

		public boolean isPagingAble() {
			return pagingAble;
		}

		public void setPagingAble(boolean pagingAble) {
			this.pagingAble = pagingAble;
		}

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public Object getDataObject() {
			return dataObject;
		}

		public void setDataObject(Object dataObject) {
			this.dataObject = dataObject;
		}

		public int getTotalCount() {
			return totalCount;
		}

		public void setTotalCount(int totalCount) {
			this.totalCount = totalCount;
		}

		public String getPageShow() {
			StringBuilder sb = new StringBuilder();
			sb.append("<ul class=\"pagination fr\">");
			/**
			 * <c:choose> <c:when test="${PG.pageIndex == 1}">
			 * <li class="disabled"><a href="javascript:void(0);">首页</a>
			 * </li> </c:when> <c:otherwise>
			 * <li class=""><a href="javascript:void(0);" onclick=
			 * "pageQuery(1,this)">首页</a></li> </c:otherwise> </c:choose>
			 **
			 */
			if (pageIndex == 1) {
				sb.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">首页</a></li>");
			} else {
				sb.append("<li class=\"\"><a href=\"javascript:void(0);\" onclick=\"pageQuery(1,this)\">首页</a></li>");
			}
			/**
			 * <c:choose> <c:when test="${PG.pageIndex == 1}">
			 * <li class="disabled"><a href="javascript:void(0);">上一页</a>
			 * </li> </c:when> <c:otherwise>
			 * <li class=""><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex - 1},this)">上一页</a>
			 * </li> </c:otherwise> </c:choose>
			 **/
			if (pageIndex == 1) {
				sb.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">上一页</a></li>");

			} else {
				sb.append("<li class=\"\"><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 1)
						+ ",this)\">上一页</a></li>");
			}
			/**
			 * <c:if test="${PG.pageIndex > 3}">
			 * <li><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex - 3},this)">${PG.pageIndex - 3}</a>
			 * </li> </c:if>
			 **/
			if(pageIndex == totalPage){
				if (pageIndex > 6) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 6) + ",this)\">"
							+ (pageIndex - 6) + "</a></li>");
				}
				if (pageIndex > 5) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 5) + ",this)\">"
							+ (pageIndex - 5) + "</a></li>");
				}
				if (pageIndex > 4) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 4) + ",this)\">"
							+ (pageIndex - 4) + "</a></li>");
				}

			}
			if(pageIndex == (totalPage- 1)){
				if (pageIndex > 5) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 5) + ",this)\">"
							+ (pageIndex - 5) + "</a></li>");
				}
				if (pageIndex > 4) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 4) + ",this)\">"
							+ (pageIndex - 4) + "</a></li>");
				}

			}
			if(pageIndex == (totalPage- 2)){
				if (pageIndex > 4) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 4) + ",this)\">"
							+ (pageIndex - 4) + "</a></li>");
				}

			}
			if (pageIndex > 3) {
				sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 3) + ",this)\">"
						+ (pageIndex - 3) + "</a></li>");
			}

			/**
			 * <c:if test="${PG.pageIndex > 2}">
			 * <li><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex - 2},this)">${PG.pageIndex - 2}</a>
			 * </li> </c:if>
			 **/
			if (pageIndex > 2) {
				sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 2) + ",this)\">"
						+ (pageIndex - 2) + "</a></li>");
			}
			/**
			 * <c:if test="${PG.pageIndex > 1}">
			 * <li><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex - 1},this)">${PG.pageIndex - 1}</a>
			 * </li> </c:if>
			 **/
			if (pageIndex > 1) {
				sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex - 1) + ",this)\">"
						+ (pageIndex - 1) + "</a></li>");
			}
			/**
			 * <li class="active"><a href="javascript:void(0);">${PG.pageIndex}
			 * </a></li>
			 **/
			sb.append("<li class=\"active\"><a href=\"javascript:void(0);\">" + (pageIndex) + "</a></li>");
			/**
			 * <c:if test="${PG.pageIndex + 1 <= PG.totalPage}">
			 * <li><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex + 1},this)">${PG.pageIndex + 1}</a>
			 * </li> </c:if>
			 **/
			if ((pageIndex + 1) <= totalPage) {
				sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 1) + ",this)\">"
						+ (pageIndex + 1) + "</a></li>");
			}
			/**
			 * <c:if test="${PG.pageIndex + 2 <= PG.totalPage}">
			 * <li><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex + 2},this)">${PG.pageIndex + 2}</a>
			 * </li> </c:if>
			 **/
			if ((pageIndex + 2) <= totalPage) {
				sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 2) + ",this)\">"
						+ (pageIndex + 2) + "</a></li>");
			}
			/**
			 * <c:if test="${PG.pageIndex + 3 <= PG.totalPage}">
			 * <li><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex + 3},this)">${PG.pageIndex + 3}</a>
			 * </li> </c:if>
			 **/
			if ((pageIndex + 3) <= totalPage) {
				sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 3) + ",this)\">"
						+ (pageIndex + 3) + "</a></li>");
			}
			if(pageIndex == 1){
				if ((pageIndex + 4) <= totalPage) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 4) + ",this)\">"
							+ (pageIndex + 4) + "</a></li>");
				}
				if ((pageIndex + 5) <= totalPage) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 5) + ",this)\">"
							+ (pageIndex + 5) + "</a></li>");
				}
				if ((pageIndex + 6) <= totalPage) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 6) + ",this)\">"
							+ (pageIndex + 6) + "</a></li>");
				}
			}
			if(pageIndex == 2){
				if ((pageIndex + 4) <= totalPage) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 4) + ",this)\">"
							+ (pageIndex + 4) + "</a></li>");
				}
				if ((pageIndex + 5) <= totalPage) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 5) + ",this)\">"
							+ (pageIndex + 5) + "</a></li>");
				}
			}
			if(pageIndex == 3){
				if ((pageIndex + 4) <= totalPage) {
					sb.append("<li><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 4) + ",this)\">"
							+ (pageIndex + 4) + "</a></li>");
				}
			}
			/**
			 * <c:choose> <c:when test="${PG.pageIndex == PG.totalPage}">
			 * <li class="disabled"><a href="javascript:void(0);">下一页</a>
			 * </li> </c:when> <c:otherwise>
			 * <li class=""><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.pageIndex + 1},this)">下一页</a>
			 * </li> </c:otherwise> </c:choose>
			 **/
			if (pageIndex == totalPage) {
				sb.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">下一页</a></li>");
			} else {
				sb.append("<li class=\"\"><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + (pageIndex + 1)
						+ ",this)\">下一页</a></li>");
			}
			/**
			 * <c:choose> <c:when test="${PG.pageIndex == PG.totalPage}">
			 * <li class="disabled"><a href="javascript:void(0);">尾页</a>
			 * </li> </c:when> <c:otherwise>
			 * <li class=""><a href="javascript:void(0);" onclick=
			 * "pageQuery(${PG.totalPage},this)">尾页</a>
			 * </li> </c:otherwise> </c:choose>
			 */
			if (pageIndex == totalPage) {
				sb.append("<li class=\"disabled\"><a href=\"javascript:void(0);\">尾页</a></li>");
			} else {
				sb.append("<li class=\"\"><a href=\"javascript:void(0);\" onclick=\"pageQuery(" + totalPage
						+ ",this)\">尾页</a></li>");
			}
			sb.append("</ul>");
			return sb.toString();
		}
	}

}
